package ro.toporjinschi.anytaxi.fragments;

import java.util.ArrayList;
import java.util.List;

import ro.toporjinschi.androidorm.EntityManager;
import ro.toporjinschi.anytaxi.R;
import ro.toporjinschi.anytaxi.database.beans.ArrayAdapaterView;
import ro.toporjinschi.anytaxi.database.beans.Place;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MyPlacesFragment extends Fragment {
    private class PlaceAdapter extends ArrayAdapter<ArrayAdapaterView> {

        public PlaceAdapter(Context context, List<ArrayAdapaterView> objects) {
            super(context, R.layout.element_places_view, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                final LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
                convertView = inflater.inflate(R.layout.element_places_view, parent, false);
            }
            final TextView tvTitle = (TextView) convertView.findViewById(R.id.title);
            final TextView tvDetails = (TextView) convertView.findViewById(R.id.details);
            tvTitle.setText(getItem(position).getBeanValueFor(R.layout.element_places_view, R.id.title));
            tvDetails.setText(getItem(position).getBeanValueFor(R.layout.element_places_view, R.id.details));
            return convertView;
        }
    }

    private ListView placesList;
    private View layout;
    private ArrayAdapter<ArrayAdapaterView> places;
    List<String> l = new ArrayList<String>();

    public MyPlacesFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.layout = inflater.inflate(R.layout.my_places, container, false);
        this.placesList = (ListView) this.layout.findViewById(R.id.llPlacesList);
        this.placesList.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        this.placesList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(view.getContext(), "Item Clicked (" + position + ")", Toast.LENGTH_SHORT).show();

            }
        });
        // DatabaseAccess db = ((AnyTaxi) getActivity().getApplication()).getDb();
        final EntityManager em = EntityManager.getInstance();
        final List<?> lp = em.get(Place.class);
        this.places = new PlaceAdapter(this.getActivity(), (List<ArrayAdapaterView>) lp);
        this.placesList.setAdapter(this.places);
        return this.layout;
    }

}
